<template>
  <view>
    <index-con />
  </view>
</template>

<script>
import indexCon from "./index-con.vue";
import { mapState } from "vuex";

export default {
  components: {
    indexCon,
  },
  computed: {
    ...mapState(["userInfo"]),
    myUid() {
      return this.userInfo.id;
    },
    userMeta() {
      return this.userInfo.user_metadata || this.userInfo;
    },
  },
  onShareAppMessage() {
    return uni.$h.getIndexShare(this.myUid);
  },
  onLoad(body = {}) {
    this.initUser(body);
  },
  onPullDownRefresh() {
    this.getUser();
  },
  onShow() {
    this.getUser();
  },
  methods: {
    async initUser({ by_uid }) {
      if (!this.myUid || this.userInfo.bot_uid) {
        await this.$store.dispatch("mpLogin");
      }
      if (
        this.myUid &&
        !this.userMeta.by_uid &&
        by_uid &&
        by_uid != this.myUid
      ) {
        await this.$store.dispatch("postUserInfo", {
          by_uid,
        });
      }
    },
    async getUser() {
      if (Date.now() - this.getUserTime < 3e3 || !this.myUid) {
        uni.stopPullDownRefresh();
        return;
      }
      try {
        await this.$store.dispatch("getUserInfo");
        this.getUserTime = Date.now();
      } catch (error) {
        console.log(error);
        if (error.status == 401) {
          this.initUser();
        }
      }
      uni.stopPullDownRefresh();
    },
  },
};
</script>
